
namespace MinCultura.Sipa.AgentesArtes
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections;
    using Common;
    using SystemFrameworks;
    using Microsoft.ApplicationBlocks.Data;

    /// <summary>
    /// Clase de acceso a datos para la entidad ART_FUENTE_INGRESO
    /// </summary>

    public class FuenteIngreso
    {

        #region propiedades

        private string _cadenaConexion;

        public string ConnectionString
        {
            get { return _cadenaConexion; }
            set { _cadenaConexion = value; }
        }

        #endregion

        #region Private Data Access Atributes
        private const String FIN_ID_PARAM = "@FIN_ID";
        private const String FIN_FUENTE_PARAM = "@FIN_FUENTE";
        #endregion Private Data Access Atributes

        /// <summary>
        /// Constructor de la clase ARTFUENTEINGRESO
        /// </summary>
        public FuenteIngreso(string strCadenaConecxion)
        {
            ConnectionString = strCadenaConecxion;
        }

        public MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData BuscarFuenteIngreso()
        {
            MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData response = new MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData();
            SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_BUSCAR_ART_FUENTE_INGRESO", response.Tables[0]);
            if (response.Tables[0].Rows.Count == 0)
                return null;
            return response;
        }


        public MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData ConsultarFuenteIngreso(decimal parFIN_ID)
        {
            MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData response = new MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData();

            SqlParameter param1 = new SqlParameter(FIN_ID_PARAM, parFIN_ID);

            SqlParameter[] parameters = { param1 };
            SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_FUENTE_INGRESO", response.Tables[0], parameters);
            if (response.Tables[0].Rows.Count == 0)
                return null;
            return response;
        }

        public decimal AdicionarFuenteIngreso(MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData datos)
        {
            DataTable tabla = datos.Tables[MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData.ART_FUENTE_INGRESO_TABLE];
            decimal decFIN_ID = 0;

            try
            {
                //StartTransaction();

                foreach (DataRow row in tabla.Rows)
                {
                    SqlParameter param1 = new SqlParameter(FIN_ID_PARAM, SqlDbType.Decimal);
                    param1.Direction = ParameterDirection.Output;
                    SqlParameter param2 = new SqlParameter(FIN_FUENTE_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData.FIN_FUENTE_FIELD]);
                    SqlParameter[] parameters = { param1, param2 };
                    SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ADICIONAR_ART_FUENTE_INGRESO", parameters);
                    decFIN_ID = decimal.Parse(param1.Value.ToString());
                }

                //if (this.RegistroAuditoria != null)
                //{
                //    this.RegistroAuditoria.Rows[0][RegistroAuditoriaData.REG_IDENTIFICADOR_FIELD] = decFIN_ID;
                //}

                //RegistrarAuditoria();

                //CompleteTransaction();

                return (decFIN_ID);
            }
            catch (SqlException sqlEx)
            {
                //AbortTransaction();
                throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "ART_FUENTE_INGRESO"), sqlEx);
            }
            catch (Exception ex)
            {
                //AbortTransaction();
                throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorInsertar", "ART_FUENTE_INGRESO"), ex);
            }
        }

        public void ActualizarFuenteIngreso(MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData datos)
        {
            DataTable tabla = datos.Tables[MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData.ART_FUENTE_INGRESO_TABLE];
            MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData fuenteIngresoDataAnt = new MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData();

            try
            {
                //StartTransaction();

                //if (this.RegistroAuditoria != null)
                //{
                //    fuenteIngresoDataAnt = ConsultarFuenteIngreso(Convert.ToDecimal(tabla.Rows[0][MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData.FIN_ID_FIELD]));
                //}

                foreach (DataRow row in tabla.Rows)
                {
                    SqlParameter param1 = new SqlParameter(FIN_ID_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData.FIN_ID_FIELD]);
                    SqlParameter param2 = new SqlParameter(FIN_FUENTE_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.FuenteIngresoData.FIN_FUENTE_FIELD]);

                    SqlParameter[] parameters = { param1, param2 };
                    SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ACTUALIZAR_ART_FUENTE_INGRESO", parameters);
                }

                //RegistrarAuditoria(fuenteIngresoDataAnt.Tables[0], tabla);

                //CompleteTransaction();
            }
            catch (SqlException sqlEx)
            {
                //AbortTransaction();
                throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "ART_FUENTE_INGRESO"), sqlEx);
            }
            catch (Exception ex)
            {
                //AbortTransaction();
                throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorActualizar", "ART_FUENTE_INGRESO"), ex);
            }
        }

        /// <summary>
        /// Elimina un registro de la entidad Artfuenteingreso
        /// </summary>
        /// <param name="parFIN_ID">Identificador del registro
        public void EliminarFuenteIngreso(decimal parFIN_ID)
        {
            try
            {
                SqlParameter param1 = new SqlParameter(FIN_ID_PARAM, parFIN_ID);

                SqlParameter[] parameters = { param1 };
                SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_FUENTE_INGRESO", parameters);
            }
            catch (SqlException sqlEx)
            {
                throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "ART_FUENTE_INGRESO"), sqlEx);
            }
            catch (Exception ex)
            {
                throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_FUENTE_INGRESO"), ex);
            }
        }

        public void EliminarFuenteIngreso(ArrayList arrIDs)
        {
            try
            {
                //StartTransaction();

                foreach (object Item in arrIDs)
                {
                    SqlParameter param1 = new SqlParameter(FIN_ID_PARAM, decimal.Parse(Item.ToString()));
                    SqlParameter[] parameters = { param1 };
                    SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_FUENTE_INGRESO", parameters);
                }

                //RegistrarAuditoria();

                //CompleteTransaction();
            }
            catch (SqlException sqlEx)
            {
                //AbortTransaction();
                throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "ART_FUENTE_INGRESO"), sqlEx);
            }
            catch (Exception ex)
            {
                //AbortTransaction();
                throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_FUENTE_INGRESO"), ex);
            }
        }
    }
}

